import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tvlives/model/category.dart';
import 'package:tvlives/pages/Live_Room_List.dart';
import 'package:tvlives/provider/provider.dart';

class RecommendPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return new RecommendationState();
  }
}

class RecommendationState extends State<RecommendPage> with SingleTickerProviderStateMixin{
  TabController _tabController;
  List<Category> _categoryList = [];


  @override
  void initState() {
     super.initState();
    _categoryList = Provider.of<CategoryList>(context,listen: false).categoryList;
    _tabController = TabController(length: _categoryList.length,vsync: this);
  }

  @override
  void dispose() {

    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(appBar: buildAppBar(),
    body: buildTabView(),);
  }

  Widget buildAppBar(){
    return AppBar(
      title: buildTabBar(),
      titleSpacing: 0.0,
      actions: <Widget>[
        new IconButton(icon: new Icon(Icons.arrow_drop_down),
            iconSize: 38.0,
            alignment: Alignment.bottomCenter,
            tooltip: "自定义",
            onPressed: (){})
      ],
    );

  }

  Widget buildTabBar(){
       List<Widget> widgets = [];
       _categoryList.forEach((category) {
         widgets.add(Tab(text:category.name));
       });
       return TabBar(
         tabs: widgets,
         isScrollable: true,
         controller: _tabController,
         indicatorColor: Colors.white,
       );
  }

  Widget buildTabView(){
      return TabBarView(
          controller: _tabController,
          children: buildTabViewItem());
  }

  List<Widget> buildTabViewItem(){
    List<Widget> widgets = [];
    _categoryList.forEach((category) {
      widgets.add(LiveList(
        cid:category.cid,
        com:"all",
      ));
    });

    return widgets;
  }

}
